/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-points
*
* @File name : EventCommitQuestionnaireHandler.java
*
* @Author : Administrator
*
* @Date : 2017年4月28日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年4月28日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/
	
package com.yonyou.gmmc.service.points.eventhandle;

import org.marketing.common.dto.EventDataDTO;
import org.marketing.common.event.BizEvent;
import org.marketing.common.event.EventCommitQuesionnaireDTO;
import org.marketing.common.event.enums.EventConstant;
import org.marketing.common.event.enums.EventConstant.EventBizStep;
import org.marketing.common.event.enums.EventConstant.EventBizType;
import org.marketing.common.util.MapConverUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yonyou.gmmc.service.points.exception.PointsException;
import com.yonyou.gmmc.service.points.model.TmPointsRule;
import com.yonyou.gmmc.service.points.service.PointsPayService;
import com.yonyou.gmmc.service.points.service.PointsRuleService;
import com.yonyou.gmmc.service.points.util.SpringUtil;

/**
*
* @author Administrator
* TODO description
* @date 2017年4月28日
*/

public class EventCommitQuestionnaireHandler extends AbstractEventHandle {

    private static Logger logger = LoggerFactory.getLogger(EventCommitQuestionnaireHandler.class);

    // 注册流程
    private static EventBizType BizType = EventConstant.EventBizType.QUESTIONNAIRE;
    // 完成注册节点
    private static EventBizStep BizStep = EventConstant.EventBizStep.QUESTIONNAIRE_COMMIT;

    // @Autowired
    // private PointsRuleService pointsRuleService;

    @Override
    protected String getHandleInfo() {
        return "问卷提交积分";
    }

    
    /**
     * 主要进行数据转换
     */
    @Override
    protected EventCommitQuesionnaireDTO preTransform(BizEvent event) {
        EventCommitQuesionnaireDTO o = new EventCommitQuesionnaireDTO();
        try {
            o = (EventCommitQuesionnaireDTO) MapConverUtil.map2POStrict(event.getEventData(), o);
            logger.info(o.toString());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            this.backInfo = "eventData中的业务数据不能识别：" + e.getMessage();
            throw new PointsException(PointsException.EVENT_CONVERT_FAILD,"eventData中的业务数据不能识别");
        }
        return o;
    }

    /**
     * 积分处理的逻辑
     */
    @Override
    protected boolean pay(EventDataDTO eventData) throws PointsException{
        
        //获取到业务数据
        EventCommitQuesionnaireDTO thisEventData = (EventCommitQuesionnaireDTO)eventData;
        
        PointsRuleService pointsRuleService=(PointsRuleService)SpringUtil.getBean("pointsRuleService");
        
        //获取到规则
//        TmPointsRule rule = pointsRuleService.getRuleByBIZ(BizType.getCodeLong(), BizStep.getCodeLong());
        //校验问卷中的积分在规则中有没有配置
        TmPointsRule rule = pointsRuleService.getRuleByID(Long.valueOf(thisEventData.getSharingPoints()));
        
        if(rule!=null){
            Long points = rule.getPointsNum();
            if(points>0){
            	PointsPayService pointsPayService=(PointsPayService)SpringUtil.getBean("pointsPayService");
                pointsPayService.payPointsToUser(points, thisEventData.getUserId(),thisEventData.getPhone(),
                        //this.getHandleInfo(),
                        rule.getRemark(),   //2017 4 13修改为规则表的remark
                        String.valueOf(thisEventData.getQneId()),
                        BizStep.getCode(),thisEventData.getPhone());
            }else{
            	logger.info(">>>>>>>问卷积分"+points+"  不做其他操作。。。。");
            }
            
        }else{
            this.backInfo = "未找到对应的积分规则：";
            throw new PointsException(PointsException.RULE_NOTFOUND,"未找到对应的积分规则");
        }
        return false;
    }



}
